﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Poco;
using Metier;
using WebApp.Models;

namespace WebApp.Controllers
{
	[Authorize(Roles="Manager")]
	public class RestaurantsController : Controller
	{

	 #region CRUD Restaurants
	    // GET: /Restaurants/
		public ActionResult Index()
	    {
		    GestionRestaurant gestRestaurant = new GestionRestaurant();

		    List<Restaurant> restaurants = gestRestaurant.Lister();

		    return View(restaurants);
	    }

		public ActionResult AjouterRestaurant()
	    {
		    return View();
	    }
		[HttpPost]
		public ActionResult AjouterRestaurant(Restaurant restaurant)
	    {
		    try
		    {
			    if (ModelState.IsValid)
			    {
				    GestionRestaurant metier = new GestionRestaurant();

				    metier.Ajouter(restaurant);
			    }

			    return RedirectToAction("Index");
		    }
		    catch
		    {
			    return View();
		    }
	    }

	    // GET: /Manager/DetailRestaurant/5
		public ActionResult Detail(int id)
	    {
		    DetailRestaurant restoMW = new DetailRestaurant();

		    GestionRestaurant resto_manager = new GestionRestaurant();
		    GestionZone zone_manager = new GestionZone();

		    restoMW.restaurant = resto_manager.Chercher(id);
		    restoMW.zones = zone_manager.Lister(restoMW.restaurant);

		    return View(restoMW);
	    }

    	// GET: /Manager/Edit/5
		public ActionResult EditerRestaurant(int id)
	{
		GestionManager gestManager = new GestionManager();
		GestionRestaurant gestResto = new GestionRestaurant();

		ViewBag.managers = gestManager.Lister();

		Restaurant resto = gestResto.Chercher(id);

		return View(resto);
	}
    	// POST: /Manager/Edit/5
		[HttpPost]
		public ActionResult EditerRestaurant(Restaurant restaurant)
	    {
		    try
		    {
			    GestionRestaurant resto_manager = new GestionRestaurant();
			    GestionManager gestManager = new GestionManager();

			    if (ModelState.IsValid)
			    {
				    //restaurant.Manager = gestManager.Chercher(restaurant.Manager. 

				    resto_manager.MettreAJour(restaurant);
			    }

			    return RedirectToAction("Detail");
		    }
		    catch
		    {
			    return View();
		    }
	    }
		//
	// GET: /Manager/Delete/5
		public ActionResult SupprimerRestaurant(int id)
	    {
		    GestionRestaurant resto_manager = new GestionRestaurant();
		    resto_manager.Supprimer(id);

		    return RedirectToAction("Index");
	    }  
	#endregion


        #region CRUD Zones
        public ActionResult AjouterZone(int id)
        {
            Zone zone = new Zone();

            zone.Restaurant = new Restaurant();

            zone.RestaurantId = id;

            return View(zone);
        }

        [HttpPost]
        public ActionResult AjouterZone(Zone zone)
        {
            try
            {
                GestionZone zone_manager = new GestionZone();
                GestionRestaurant resto_manager = new GestionRestaurant();

                if (ModelState.IsValid)
                {
                    zone_manager.Ajouter(zone);
                }

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return View();
            }
        }

        public ActionResult DetailZone(int id)
        {
            GestionZone zone_manager = new GestionZone();

            Zone zone = zone_manager.Chercher(id);

            return View(zone);
        }

        public ActionResult EditerZone(int id)
        {
            GestionZone zone_manager = new GestionZone();
            GestionServeur serveur_manager = new GestionServeur();

            Zone zone = zone_manager.Chercher(id);

            List<Serveur> serveurs = serveur_manager.Lister(zone.Restaurant);

            ViewBag.serveurs = serveurs;

            return View(zone);
        }
        [HttpPost]
        public ActionResult EditerZone(Zone zone)
        {
            try
            {
                GestionZone zone_manager = new GestionZone();

                if (ModelState.IsValid)
                {
                    zone_manager.MettreAJour(zone);
                }

                return RedirectToAction("Detail");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult SupprimerZone(int id)
        {
            GestionZone zone_manager = new GestionZone();

            zone_manager.Supprimer(id);

            return RedirectToAction("Detail");
        } 
        #endregion

        #region CRUD Tables

        public PartialViewResult AjouterTable(int id)
        {
            Table table = new Table();

            table.ZoneId = id;

            return PartialView("_AjouterTable", table);
        }

        [HttpPost]
        public PartialViewResult AjouterTable(Table table)
        {
            try
            {
                GestionTable table_manager = new GestionTable();

                if (ModelState.IsValid)
                {
                    table_manager.Ajouter(table);
                }

                return PartialView("_Table", table);
            }
            catch (Exception ex)
            {
                return PartialView();
            }
        }

        public ActionResult EditerTable(int id)
        {
            GestionTable table_manager = new GestionTable();

            Table table = table_manager.Chercher(id);

            return View(table);
        }

        [HttpPost]
        public ActionResult EditerTable(Table table)
        {
            try
            {
                GestionTable table_manager = new GestionTable();

                if (ModelState.IsValid)
                {
                    table_manager.MettreAJour(table);
                }

                return RedirectToAction("DetailZone");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult SupprimerTable(int id)
        {
            GestionTable table_manager = new GestionTable();

            table_manager.Supprimer(id);

            return RedirectToAction("DetailZone");
        } 
        #endregion
	}
}
